package org.basis.algorithm.recursion;

/**
 * 统计特殊四元组
 *
 * @author Mr_wenpan@163.com 2021/12/29 21:25
 */
public class CountQuadruplets {

    public static void main(String[] args) {
        int[] nums = {1, 1, 1, 3, 5};
        int res = countQuadruplets(nums);
        System.out.println(res);
    }

    public static int countQuadruplets(int[] nums) {
        return doProcess3(nums, 0, 0, 0);
    }

    public static int doProcess3(int[] nums, int i, int index, int sum) {
        // 走到终点或者已经凑齐三个数了就结束
        if (index == nums.length || i == 3) {
            // 走到结尾了，那么直接返回0种
            if (index == nums.length) {
                return 0;
            }
            int num = 0;
            // 看看index后面的数是否有等于前三个数之和的数
            for (int j = index; j < nums.length; j++) {
                if (sum == nums[j]) {
                    num++;
                }
            }
            // 结算
            return num;
        }
        int res1 = 0;
        int res2 = 0;
        // 要当前数
        res1 += doProcess3(nums, i + 1, index + 1, sum + nums[index]);
        // 不要当前数
        res2 += doProcess3(nums, i, index + 1, sum);

        return res1 + res2;
    }

}
